<?php
/**
 * ClassName:仓库历史处理器
 * @auditor:leijunxiang
 * @time 2025/4/11 15:46
 */

namespace app\tool\freightForward;

use app\modelOperate\freightForward\WarehousePriceHistoryOpe;
use app\tool\ToolBase;
use think\facade\Log;

class HistoryPriceHandle extends ToolBase
{
    /**
     * 开始查询(详情id,开始时间,结束时间,页数,条数)
     */
    public function startSelect($detailId, $startTime, $endTime, $page, $limit)
    {
        //查询历史(详情id,开始时间,结束时间,页数,条数)
        $arr = $this->selectHistory($detailId, $startTime, $endTime, $page, $limit);
        //填充历史数据(历史数据)
        $data['historyAttr'] = $this->fillHistoryData($arr);
        $count = 0;
        if ($page <= 1) {
            //查询总数(详情id,开始时间,结束时间)
            $count = $this->selectCount($detailId, $startTime, $endTime,$limit);
        }
        $data['totalCount'] = $count;
        return $data;
    }
    /**
     * 开始查询(详情id,开始时间,结束时间,页数,条数)
     */
    public function startSelectPrice($detailId, $startTime, $endTime)
    {
        $ope = app(WarehousePriceHistoryOpe::class);
        //查询详情数据(详情id,开始时间,结束时间)
        $arr = $ope->selectData($detailId, $startTime, $endTime);
        if (empty($arr)) {
            return [];
        }
        //填充历史数据(历史数据)
        $data['historyAttr'] = $this->fillHistoryData($arr);
        return $data;
    }

    /**
     * 查询历史(详情id,开始时间,结束时间,页数,条数)
     */
    protected function selectHistory($detailId, $startTime, $endTime, $page, $limit)
    {
        //查询3倍的数据
        $limit = $limit * 3;
        $ope = app(WarehousePriceHistoryOpe::class);
        //查询详情数据(详情id,开始时间,结束时间,页数,条数)
        $data = $ope->selectDataWithPagination($detailId, $startTime, $endTime, $page, $limit);
        if (empty($data)) {
            return [];
        }
        return $data;
    }

    /**
     * 填充历史数据(历史数据)
     */
    protected function fillHistoryData($arr)
    {
        $data = [];
        if (empty($arr)) {
            return [];
        }
        $dateList = [];
        //整理历史数据
        foreach ($arr as $item) {
            $time = $item['time'];
            if (!in_array($time, $dateList)) {
                $dateList[] = $time;
            }
            $data[$time][] = $item;
        }
//        //找到完整字段数
//        $len = 0;
//        foreach ($data as $arr) {
//            if ($len < count($arr)) {
//                $len = count($arr);
//            }
//        }
//        //补充数组
//        $bcAttr = [];
//        //完整数组
//        $perfection = [];
//        //补充日期数组
//        $arrDate = [];
//        //找到需要补充的字段
//        for ($i = 0; $i < count($data); $i++) {
//            $item = $data[$dateList[$i]];
//            if (count($item) < $len) {
//                $time = $item[0]['time'];
//                $bcAttr[$time] = $item;
//                $arrDate[] = $time;
//                //获取完整字段
//                for ($j = $i + 1; $j < count($data); $j++) {
//                    if(count($data[$dateList[$j]])<$len){
//                        continue;
//                    }
//                    $perfection[$time] = $data[$dateList[$j]];
//                    break;
//                }
//            }
//        }
//        if ($bcAttr && $perfection && $arrDate) {
//            //合并
//            $megAttr = $this->mergeDateData($bcAttr, $perfection, $arrDate);
//            foreach ($megAttr as $date => $item) {
//                $data[$date] = $item;
//            }
//        }
        return $data;
    }

    /**
     * 查询总数(详情id,开始时间,结束时间)
     * @return void
     */
    protected function selectCount($detailId, $startTime, $endTime,$limit)
    {
        $ope = app(WarehousePriceHistoryOpe::class);
        //查询总数(详情id,开始时间,结束时间)
        $count = $ope->selectCount($detailId, $startTime, $endTime);
        return $count;
    }

    /**
     * 合并两个日期的数据(需补充数据，完整数据,完整日期)
     */
    protected function mergeDateData($bcAttr, $perfection, $arrDate)
    {
        if (empty($bcAttr) || empty($perfection) || empty($arrDate)) {
            return [];
        }

        foreach ($arrDate as $date) {
            $item = $bcAttr[$date];
            $per = $perfection[$date];
            // 提取 $date1 中的所有重量
            $date1Weights = array_column($bcAttr[$date], 'weight');
            $newData = [];
            foreach ($per as $value) {
                if (!in_array($value['weight'], $date1Weights)) {
                    $value['time'] = $date;
                    $newData[] = $value;
                }
            }
            // 将新数据合并到 $date1 中
            $data[$date] = array_merge($item, $newData);
        }
        return $data;
    }


}
